package com.shaoshaohuo.app.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.OrderShopInfoEntity;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.ui.ApplyBackMoney;
import com.shaoshaohuo.app.ui.CommitPayOrder;
import com.shaoshaohuo.app.ui.EvaluateActivity;
import com.shaoshaohuo.app.ui.MapZootm;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.utils.ToastUtil;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/6/9.
 */

public class AllOrderListViewAdapter extends BaseAdapter implements View.OnClickListener {
    private List<OrderShopInfoEntity.DataBean> data;
    private Context context;
    private String order_item;
    private Handler handler = new Handler();

    private String status;
    private String money;
    private CallBackListener callBackListener;

    public void setCallBackListener(CallBackListener callBackListener) {
        this.callBackListener = callBackListener;
    }

    public AllOrderListViewAdapter(List<OrderShopInfoEntity.DataBean> data, Context context, String order_item) {
        this.data = data;
        this.context = context;
        this.order_item = order_item;
    }

    public void refreshAdapter(List<OrderShopInfoEntity.DataBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void addload(List<OrderShopInfoEntity.DataBean> all_order_listView_data) {
        this.data.addAll(all_order_listView_data);
        notifyDataSetChanged();

    }

    public List<OrderShopInfoEntity.DataBean> getData() {
        return data;
    }

    public void clearData() {
        data.clear();
    }

    @Override

    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public OrderShopInfoEntity.DataBean getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AllOrderItemViewHolder holder = null;
        if (convertView == null) {

            convertView = LayoutInflater.from(context).inflate(R.layout.all_order_item, parent, false);
            holder = new AllOrderItemViewHolder();
            holder.order_shop_active = (TextView) convertView.findViewById(R.id.order_shop_active);
            holder.order_shop_name = (TextView) convertView.findViewById(R.id.order_shop_name);
            holder.order_shop_details = (TextView) convertView.findViewById(R.id.order_shop_details);
            holder.order_shop_money = (TextView) convertView.findViewById(R.id.order_shop_money);
            holder.order_shop_mbn_details = (TextView) convertView.findViewById(R.id.order_shop_mbn_details);
            holder.order_shop_cancel = (TextView) convertView.findViewById(R.id.order_shop_cancel);
            holder.order_shop_picture = (ImageView) convertView.findViewById(R.id.order_shop_picture);
            holder.order_shop_hint = (TextView) convertView.findViewById(R.id.order_shop_hint);
            holder.order_shop_hour = (TextView) convertView.findViewById(R.id.order_shop_hour);
            holder.order_shop_apply_back_money = (TextView) convertView.findViewById(R.id.order_shop_apply_back_money);
            holder.sure_order_shop_pick_address = (TextView) convertView.findViewById(R.id.sure_order_shop_pick_address);
            holder.order_shop_hint_peisong_lenglian = (LinearLayout) convertView.findViewById(R.id.order_shop_hint_peisong_lenglian);
            holder.sure_order_shop_shanghu_address = (LinearLayout) convertView.findViewById(R.id.sure_order_shop_shanghu_address);
            holder.order_shop_guoqi = (ImageView) convertView.findViewById(R.id.order_shop_guoqi);
            convertView.setTag(holder);
        } else {
            holder = (AllOrderItemViewHolder) convertView.getTag();
        }
        if (data.get(position).getType() == 1) {
            holder.sure_order_shop_shanghu_address.setVisibility(View.GONE);
            holder.order_shop_hint_peisong_lenglian.setVisibility(View.VISIBLE);
            holder.order_shop_mbn_details.setVisibility(View.VISIBLE);
            holder.order_shop_details.setVisibility(View.VISIBLE);
            holder.order_shop_mbn_details.setText(data.get(position).getMbn_details());
            holder.order_shop_details.setText("(一件起售 " + data.get(position).getDetails() + ")");
        } else if (data.get(position).getType() == 2) {
            holder.sure_order_shop_shanghu_address.setVisibility(View.VISIBLE);
            holder.order_shop_hint_peisong_lenglian.setVisibility(View.GONE);
            holder.order_shop_mbn_details.setVisibility(View.GONE);
            holder.order_shop_details.setVisibility(View.GONE);
            holder.sure_order_shop_pick_address.setText(data.get(position).getAddress());
        }

//        SpannableString spanMoney = new SpannableString("¥" + data.get(position).getFin_money());
//        AbsoluteSizeSpan span = new AbsoluteSizeSpan(30);
//        spanMoney.setSpan(span, 1, 3, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        Picasso.with(context).load(data.get(position).getImg()).into(holder.order_shop_picture);
        holder.order_shop_name.setText(data.get(position).getName());
        holder.order_shop_money.setText("¥ " + data.get(position).getFin_money() + "/单");
        holder.order_shop_hour.setText(data.get(position).getOrder_time());
        holder.order_shop_active.setTag(position);
        holder.order_shop_apply_back_money.setTag(position);
        holder.order_shop_cancel.setTag(position);
        decideOrder(holder, data.get(position).getStatus(), position);
        if (order_item.equals("全部")) {
            String status = data.get(position).getStatus();
            holder.order_shop_hint.setText(hintMessage(status, holder, position));
//            decideOrder(holder, status, position);
        }
        holder.order_shop_active.setOnClickListener(this);
        holder.order_shop_apply_back_money.setOnClickListener(this);
        holder.order_shop_cancel.setOnClickListener(this);
        return convertView;
    }


    private void decideOrder(AllOrderItemViewHolder holder, String hint, int position) {
        if (order_item.equals("待付款")) {
            if (data.get(position).getGoodStatus().equals("2")) {
                holder.order_shop_guoqi.setVisibility(View.VISIBLE);
                holder.order_shop_guoqi.setImageResource(R.mipmap.yiguoqi);
            } else {
                holder.order_shop_guoqi.setVisibility(View.GONE);
            }
            holder.order_shop_apply_back_money.setVisibility(View.GONE);
            holder.order_shop_cancel.setVisibility(View.VISIBLE);
            holder.order_shop_active.setVisibility(View.VISIBLE);
            holder.order_shop_cancel.setText("取消订单");
            holder.order_shop_active.setText("去支付");
            holder.order_shop_hint.setText("待支付");
        } else if (order_item.equals("待发货")) {
            holder.order_shop_guoqi.setVisibility(View.GONE);
            if (decideType(position)) {
                holder.order_shop_apply_back_money.setVisibility(View.VISIBLE);
                holder.order_shop_active.setVisibility(View.VISIBLE);
                holder.order_shop_active.setText("提醒发货");
                holder.order_shop_cancel.setVisibility(View.GONE);
                holder.order_shop_hint.setText("待发货");
            }
        } else if (order_item.equals("待确认")) {
            holder.order_shop_guoqi.setVisibility(View.GONE);
            if (data.get(position).getType() == 1) {
                holder.order_shop_apply_back_money.setVisibility(View.VISIBLE);
                holder.order_shop_cancel.setVisibility(View.VISIBLE);
                holder.order_shop_active.setVisibility(View.VISIBLE);
                holder.order_shop_cancel.setText("查看物流");
                holder.order_shop_active.setText("确认收货");
                holder.order_shop_hint.setText("待确认");
            } else if (data.get(position).getType() == 2) {
                holder.order_shop_apply_back_money.setVisibility(View.VISIBLE);
                holder.order_shop_cancel.setVisibility(View.GONE);
                holder.order_shop_active.setVisibility(View.VISIBLE);
                holder.order_shop_active.setText("确认入园");
                holder.order_shop_hint.setText("待确认");
            }
        } else if (order_item.equals("已完成")) {
            holder.order_shop_guoqi.setVisibility(View.GONE);
            holder.order_shop_apply_back_money.setVisibility(View.GONE);
//            holder.order_shop_cancel.setVisibility(View.VISIBLE);
            holder.order_shop_active.setVisibility(View.VISIBLE);
            holder.order_shop_cancel.setVisibility(View.GONE);
            if (data.get(position).getIsmark() == 1) {
                holder.order_shop_active.setText("已评价");
            } else if (data.get(position).getIsmark() == 2) {
                holder.order_shop_active.setText("立即评价");
            }
            holder.order_shop_hint.setText("交易成功");
        } else if (order_item.equals("待退款")) {
            holder.order_shop_guoqi.setVisibility(View.GONE);
            holder.order_shop_apply_back_money.setVisibility(View.GONE);
            holder.order_shop_cancel.setVisibility(View.GONE);
            holder.order_shop_active.setVisibility(View.GONE);
            holder.order_shop_hint.setText(hintMessage(hint, holder, position));
        }
    }

    private boolean decideType(int pos) {
        if (data.get(pos).getType() == 1) {
            return true;
        }
        return false;
    }

    private String hintMessage(String hint, AllOrderItemViewHolder holder, int pos) {
        if (hint.equals("-1")) {
            holder.order_shop_cancel.setVisibility(View.GONE);
            holder.order_shop_active.setVisibility(View.VISIBLE);
            holder.order_shop_active.setText("删除订单");
            holder.order_shop_guoqi.setVisibility(View.GONE);
            holder.order_shop_apply_back_money.setVisibility(View.GONE);
            return "交易关闭";
        } else if (hint.equals("1")) {
            holder.order_shop_apply_back_money.setVisibility(View.GONE);
            holder.order_shop_cancel.setVisibility(View.VISIBLE);
            holder.order_shop_active.setVisibility(View.VISIBLE);
            if (data.get(pos).getGoodStatus().equals("2")) {
                holder.order_shop_guoqi.setVisibility(View.VISIBLE);
                holder.order_shop_guoqi.setImageResource(R.mipmap.yiguoqi);
            } else {
                holder.order_shop_guoqi.setVisibility(View.GONE);
            }
            holder.order_shop_cancel.setText("取消订单");
            holder.order_shop_active.setText("去支付");
            return "待支付";
        } else if (hint.equals("2")) {
            holder.order_shop_guoqi.setVisibility(View.GONE);
            holder.order_shop_apply_back_money.setVisibility(View.VISIBLE);
            holder.order_shop_active.setVisibility(View.VISIBLE);
            holder.order_shop_active.setText("提醒发货");
            holder.order_shop_cancel.setVisibility(View.GONE);
            return "待发货";
        } else if (hint.equals("3")) {
            holder.order_shop_guoqi.setVisibility(View.GONE);
            if (data.get(pos).getType() == 1) {
                holder.order_shop_apply_back_money.setVisibility(View.VISIBLE);
                holder.order_shop_cancel.setVisibility(View.VISIBLE);
                holder.order_shop_active.setVisibility(View.VISIBLE);
                holder.order_shop_cancel.setText("查看物流");
                holder.order_shop_active.setText("确认收货");
                return "待确认";
            } else if (data.get(pos).getType() == 2) {
                holder.order_shop_apply_back_money.setVisibility(View.VISIBLE);
                holder.order_shop_cancel.setVisibility(View.GONE);
                holder.order_shop_active.setVisibility(View.VISIBLE);
                holder.order_shop_active.setText("确认入园");
                return "待确认";
            }
        } else if (hint.equals("4")) {
            holder.order_shop_guoqi.setVisibility(View.GONE);
            holder.order_shop_apply_back_money.setVisibility(View.GONE);
            holder.order_shop_cancel.setVisibility(View.GONE);
            holder.order_shop_active.setVisibility(View.GONE);
            return "退款中";
        } else if (hint.equals("5")) {
            holder.order_shop_guoqi.setVisibility(View.GONE);
            holder.order_shop_apply_back_money.setVisibility(View.GONE);
            holder.order_shop_cancel.setVisibility(View.GONE);
            holder.order_shop_active.setVisibility(View.VISIBLE);
            holder.order_shop_active.setText("立即评价");
            return "交易成功";
        } else if (hint.equals("6")) {
            holder.order_shop_guoqi.setVisibility(View.GONE);
            holder.order_shop_apply_back_money.setVisibility(View.GONE);
            holder.order_shop_cancel.setVisibility(View.GONE);
            holder.order_shop_active.setVisibility(View.GONE);
            return "退款成功";
        } else if (hint.equals("0")) {
            holder.order_shop_guoqi.setVisibility(View.GONE);
            return "购物车状态";
        } else if (hint.equals("8")) {
            holder.order_shop_guoqi.setVisibility(View.GONE);
            holder.order_shop_apply_back_money.setVisibility(View.GONE);
            holder.order_shop_cancel.setVisibility(View.GONE);
            holder.order_shop_active.setVisibility(View.GONE);
            return "退款失败";
        } else if (hint.equals("7")) {
            holder.order_shop_guoqi.setVisibility(View.GONE);
            holder.order_shop_apply_back_money.setVisibility(View.GONE);
            holder.order_shop_cancel.setVisibility(View.GONE);
            holder.order_shop_active.setVisibility(View.VISIBLE);
            if (data.get(pos).getIsmark() == 2) {
                holder.order_shop_active.setText("立即评价");
            }
            if (data.get(pos).getIsmark() == 1) {
                holder.order_shop_active.setText("已评价");
            }
            return "交易成功";
        }
        return "";
    }

    @Override
    public void onClick(View v) {
        final int position = (int) v.getTag();
//        final AllOrderItemViewHolder holder = (AllOrderItemViewHolder) v.getTag();
        Intent intent = new Intent();

        if (data.get(position).getFin_money() != null) {
            money = "" + Double.valueOf(data.get(position).getFin_money());
        }
        status = data.get(position).getStatus();//type
        switch (v.getId()) {
            case R.id.order_shop_active:
                if (order_item.equals("全部")) {
                    if (status.equals("1")) {
                        if (data.get(position).getGoodStatus().equals("2")) {
                            ToastUtil.toast("商品已过期");
                        } else {
                            intent.setClass(context, new CommitPayOrder().newInstance(intent, money, data.get(position).getOrderid()).getClass());
                            context.startActivity(intent);
                        }
                    } else if (status.equals("2")) {
                        Toast.makeText(context, "提醒成功，我们会尽快为您发货。", Toast.LENGTH_SHORT).show();
                    } else if (status.equals("3")) {
                        if (data.get(position).getType() == 1) {
//                            Toast.makeText(context, "确认收货", Toast.LENGTH_SHORT).show();
                            ConfirmReceipt(position, "1");
                        } else if (data.get(position).getType() == 2) {
//                            Toast.makeText(context, "确认入园", Toast.LENGTH_SHORT).show();
                            ConfirmReceipt(position, "3");
                        }
                    } else if (status.equals("-1")) {
//                        Toast.makeText(context, "删除订单", Toast.LENGTH_SHORT).show();
                        DeleteOrder(position);
                    } else if (status.equals("5")) {
                        if (data.get(position).getIsmark() == 1) {
                            Toast.makeText(context, "你已经评价", Toast.LENGTH_SHORT).show();
                        } else if (data.get(position).getIsmark() == 2) {
                            intent.setClass(context, EvaluateActivity.class);
                            intent.putExtra("picture", data.get(position).getImg());
                            intent.putExtra("catid", data.get(position).getCatid());
                            context.startActivity(intent);
                        }
                    } else if (status.equals("7")) {
                        if (data.get(position).getIsmark() == 1) {
                            Toast.makeText(context, "你已经评价", Toast.LENGTH_SHORT).show();
                        } else if (data.get(position).getIsmark() == 2) {
                            intent.setClass(context, EvaluateActivity.class);
                            intent.putExtra("picture", data.get(position).getImg());
                            intent.putExtra("catid", data.get(position).getOrderid());
                            context.startActivity(intent);
                        }
                    }
                } else {
                    if (order_item.equals("待付款")) {
                        if (data.get(position).getGoodStatus().equals("2")) {
                            ToastUtil.toast("商品已过期");
                        } else {
                            intent.setClass(context, new CommitPayOrder().newInstance(intent, money, data.get(position).getOrderid()).getClass());
                            context.startActivity(intent);
                        }
                    } else if (order_item.equals("待发货")) {
                        Toast.makeText(context, "提醒成功，我们会尽快为您发货。", Toast.LENGTH_SHORT).show();
                    } else if (order_item.equals("待确认")) {
                        if (data.get(position).getType() == 1) {
//                            Toast.makeText(context, "确认收货", Toast.LENGTH_SHORT).show();
                            ConfirmReceipt(position, "1");
                        } else if (data.get(position).getType() == 2) {
//                            Toast.makeText(context, "确认入园", Toast.LENGTH_SHORT).show();
                            ConfirmReceipt(position, "3");
                        }
                    } else if (order_item.equals("已完成")) {
                        if (data.get(position).getIsmark() == 1) {
                            Toast.makeText(context, "你已经评价", Toast.LENGTH_SHORT).show();
                        } else if (data.get(position).getIsmark() == 2) {
                            intent.setClass(context, EvaluateActivity.class);
                            intent.putExtra("picture", data.get(position).getImg());
                            intent.putExtra("catid", data.get(position).getOrderid());
                            context.startActivity(intent);
                        }
                    }
                }
                break;
            case R.id.order_shop_apply_back_money:
                applyBackMoney(position, intent);
                break;
            case R.id.order_shop_cancel:
                if (order_item.equals("全部")) {
                    if (status.equals("1")) {
//                        Toast.makeText(context, "取消订单", Toast.LENGTH_SHORT).show();
                        CancelOrder(position);
                    } else if (status.equals("3")) {
//                        Toast.makeText(context, "查看物流", Toast.LENGTH_SHORT).show();
                        Intent intent1 = new Intent();
                        intent1.setClass(context, MapZootm.class);
                        intent1.putExtra("orderid", data.get(position).getOrderid());
                        context.startActivity(intent1);
                    } else if (status.equals("7")) {
                    }
                } else {
                    if (order_item.equals("待付款")) {
//                        Toast.makeText(context, "取消订单", Toast.LENGTH_SHORT).show();
                        CancelOrder(position);
                    } else if (order_item.equals("待确认")) {
//                        Toast.makeText(context, "查看物流", Toast.LENGTH_SHORT).show();
                        Intent intent1 = new Intent();
                        intent1.setClass(context, MapZootm.class);
                        intent1.putExtra("orderid", data.get(position).getOrderid());
                        context.startActivity(intent1);
                    }
                }
                break;
        }
    }

    private void applyBackMoney(final int position, Intent intent) {
        if (decideType(position)) {
            intent.setClass(context, new ApplyBackMoney().newInstance(intent, data.get(position)).getClass());
            context.startActivity(intent);
        } else {
            AlertDialog.Builder dialog = new AlertDialog.Builder(context);
            dialog.setTitle("申请退款");
            dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //不跳界面网络请求
                    ShopHttpConfig.applyBackMoney(context, null, "3", data.get(position).getOrderid(), "", "", "", new Okhttputils.OkutListener() {
                        @Override
                        public void error(Call call, IOException e) {

                        }

                        @Override
                        public void error(Call call, int code) {
                            Log.e("caizhia", code + "失败");
                        }

                        @Override
                        public void suseff(Call call, Response response, Object o) {

                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(context, "申请成功", Toast.LENGTH_SHORT).show();
                                    callBackListener.refreshData(order_item);
                                }
                            });
                        }
                    }, BaseEntity.class);
                }
            });
            dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            dialog.create().show();
        }
    }

    //确认收货
    private void ConfirmReceipt(final int position, final String type) {
        ShopHttpConfig.ConfirmReceipt(context, type, data.get(position).getOrderid(), new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {
                Log.e("确认收货", "收货失败");
                handler.post(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                Log.e("确认收货", "收货成功");
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "确认成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent();
                        intent.setAction("daifukuan");
                        context.sendBroadcast(intent);
                    }
                });


            }
        }, BaseEntity.class);
    }

    //取消订单
    private void CancelOrder(final int position) {

        ShopHttpConfig.CancleOrder(context, position, data, new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {
//                                Toast.makeText(context, "取消失败", Toast.LENGTH_SHORT).show();
                Log.e("取消订单", "取消失败");
            }

            @Override
            public void suseff(Call call, Response response, Object o) {

                Log.e("取消订单", "取消成功");
                handler.post(new Runnable() {
                    @Override
                    public void run() {
//                        refreshAdapter(data);
//                        notifyDataSetChanged();
//                        if (order_item.equals("待付款")) {
//                            callBackListener.refreshData(order_item);
//                            Intent intent = new Intent();
//                            intent.setAction("daifukuan");
//                            context.sendBroadcast(intent);
//                        } else if (order_item.equals("全部")) {
                        Toast.makeText(context, "取消成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent();
                        intent.setAction("daifukuan");
                        context.sendBroadcast(intent);

//                        }
                    }
                });
            }
        }, OrderShopInfoEntity.class);
    }

    //删除订单
    private void DeleteOrder(final int position) {
        ShopHttpConfig.DeleteOrder(context, position, data, new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {
                Log.e("删除订单", "删除失败");
            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                Log.e("删除订单", "删除成功");
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBackListener.refreshData(order_item);
                    }
                });
            }
        }, OrderShopInfoEntity.class);
    }

    public interface CallBackListener {
        void refreshData(String type);
    }


    class AllOrderItemViewHolder {
        TextView order_shop_hint;
        TextView order_shop_hour;
        TextView order_shop_name;
        TextView order_shop_details;
        TextView order_shop_money;
        TextView order_shop_mbn_details;
        TextView order_shop_cancel;
        TextView order_shop_active;
        ImageView order_shop_picture;
        TextView order_shop_apply_back_money;

        LinearLayout sure_order_shop_shanghu_address;
        LinearLayout order_shop_hint_peisong_lenglian;
        TextView sure_order_shop_pick_address;
        ImageView order_shop_guoqi;
    }
}
